<?php
App::import('Sanitize');

class UsersController extends AppController
{
	var $name = 'Users';
	var $helpers = array('Form','Html','Javascript');
    var $components = array('RequestHandler', 'Security');
    var $uses = array('User', 'Currency');
    
	function beforeFilter()
    {
        parent::beforeFilter();
		$this->Auth->allow('register');
    }
	
	function index()
	{
		if ($this->Auth->user() != null)
		{
			$this->redirect('dashboard');
			exit();
      	}
      	else
      	{
			$this->redirect('login');
			exit();
      	}
	}
	
    function login()
    {
    	
    }

    function logout()
    {
        $this->redirect($this->Auth->logout());
    }
    
    function dashboard()
    {
    	$this->set('user', $this->Auth->user());
    	$this->set('currency', $this->Currency->findAll());
    }
    
    function register()
    {
	    //If the user is all ready logged in, take them to their homepage.
      	if ($this->Auth->user() == null)
		{
			//If they have posted the form in then process it.
			if(!empty($this->data))
			{
          		$sanitize = new Sanitize();
				$this->data['User'] = Sanitize::clean($this->data['User']);
          
				//Attempt to register the user
				$user = $this->User->register($this->data['User']);
				if($user)
				{
					//Log the user in
					$this->Auth->login($user);
					
					//Take them to the home page
					$this->redirect('dashboard');
					exit();
				}
				else
				{
            		//Reg fail
            		//Set an error message
	            	//exit();
				}
			}
		}
		else
		{
			//Take them to the home page
 			$this->redirect('dashboard');
  			exit();
      	}
    }
}
?>